/*
dialog_control();
*/
global.dialog = dialog_active;
dialog_active = (ds_list_size(dialog_queue[0]) > 0) || (dialog_linetype > 0);
if (dialog_active == true) && (trigger_get("shop") == false)
{
    dialog_lineended = (dialog_textposition[0] >= string_length(dialog_text[0])) || (string_length(dialog_value[0]) <= 0 && dialog_linetype != 1);
    if (dialog_linetype < 1)
    {
        dialog_dequeue();
    }
    else
    {
        if ((dialog_lastcondition == 8) || (dialog_lastcondition == 9) || (dialog_lastcondition == 12))
        {
            dialog_nextline = false;
            var n, dialog_condition, dialog_condition_boolean;
            dialog_condition[0,0] = 8;
            dialog_condition[0,1] = (trigger_get(dialog_value[0]) == dialog_value[1]);
            dialog_condition[1,0] = 9;
            dialog_condition[1,1] = (variable_get(dialog_value[0]) == dialog_value[1]);
            dialog_condition[2,0] = 12;
            dialog_condition[2,1] = (team_member_exists(real(dialog_value[0])) == dialog_value[1]);
            dialog_condition[3,0] = 13;
            dialog_condition[3,1] = (inventory_item_cancarry(real(dialog_value[0]), 1) == dialog_value[1]);
            dialog_condition_boolean = false;
            for (n = 0; n < array_height_2d(dialog_condition); n++)
            {
                if ((dialog_lastcondition == dialog_condition[n,0]) && (dialog_condition_boolean == false))
                {
                    dialog_condition_boolean = (dialog_condition[n,1]);
                }
            }
            if (dialog_condition_boolean == true)
            {
                dialog_lastcondition = -1;
                dialog_executeline = true;
                dialog_dequeue();
            }
            else
            {
                dialog_dequeue();
                dialog_executeline = (dialog_linetype == 10);
            }
        }
        else
        {
            dialog_executeline = true;
            if (dialog_lineended == true) && (dialog_nextline == true)
            {
                if (string_length(dialog_text[0])>0)
                {
                    dialog_lasttext = dialog_text[1];
                    dialog_text[0] = "";
                    dialog_text[1] = "";
                }
                dialog_dequeue();
                dialog_textposition[0] = 0;
                dialog_textposition[1] = 0;
                dialog_executeline = false;
                dialog_nextline = false;
                dialog_linended = false;
                dialog_option_selected = 0;
                dialog_timer = 0;
                dialog_paused = false;
                dialog_pausetime = 0;
            }
        }
    }
    if (dialog_executeline == true){
        switch (dialog_linetype)
        {
            case 1:
            //Normal text
            if (string(dialog_value[0]) != string(dialog_text[0]))
            {
                dialog_text[0] = dialog_value[0];
                dialog_text[1] = dialog_filtered_text(dialog_value[0]);
            }
            if (dialog_textposition[0] <= string_length(dialog_text[0]))
            {
                if (dialog_pausetime <= 0)
                {
                    if (dialog_hidden_character(string_char_at(dialog_text[0], dialog_textposition[0] + 1)) == false)
                    {
                        dialog_textposition[0] += 1;
                        dialog_textposition[1] += 1;
                    }
                    else
                    {
                        if (dialog_pausetimer == -1) and (dialog_paused == false)
                        {
                            switch (string_char_at(dialog_text[0], dialog_textposition[0] + 1))
                            {
                                case "@":
                                dialog_pausetimer = floor(room_speed / 4);
                                break;
                                case "|":
                                dialog_paused = true;
                                break;
                            }
                        }
                        else
                        {
                            if (dialog_paused == true)
                            {
                                if (global.button_anykey_pressed == true)
                                {
                                    dialog_textposition[0] += 1;
                                    dialog_paused = false;
                                }
                            }
                            else
                            {
                                if (dialog_pausetimer == 0)
                                {
                                    dialog_textposition[0] += 1;
                                    dialog_pausetimer = -1;
                                }
                                else
                                {
                                    dialog_pausetimer--;    
                                }
                            }
                        }
                    }
                    if (dialog_value[1] == true)
                    {
                        dialog_timer = 1;
                    }
                    dialog_pausetime = (room_speed / 128);
                }
                else
                {
                    dialog_pausetime -= (30 / room_speed);
                }
            }
            else
            {
                if (dialog_value[1] == true)
                {
                    if (dialog_timer < room_speed)
                    {
                        dialog_timer += 1;
                    }
                    else
                    {
                        dialog_nextline = true;
                    }
                }
            }
            break;
            
            case 2:
            //Name on dialog
            if (is_real(dialog_value[0]) == true)
            {
                if (dialog_value[0] < 0)
                {
                    dialog_name = "";
                }
                else
                {
                    dialog_name = dialog_currentlytalking;
                }
            }
            else
            {
                dialog_name = dialog_value[0];
            }
            dialog_nextline = true;
            break;
            
            case 3:
            //Mugshot on dialog
            if (dialog_value[0] < 0)
            {
                dialog_mugshot = -1;
            }
            else
            {
                dialog_mugshot = dialog_value[0];
            }
            dialog_nextline = true;
            break;
            case 4:
            //Set trigger
            trigger_set(dialog_value[0],dialog_value[1]);
            dialog_nextline = true;
            break;
            case 5:
            //Set variable
            variable_set(dialog_value[0],dialog_value[1]);
            dialog_nextline = true;
            break;
            case 6:
            //Give an item
            inventory_item_give(dialog_value[0],dialog_value[1]);
            dialog_nextline = true;
            break;
            case 7:
            //Join a member to the team
            team_member_add(dialog_value[0]);
            dialog_nextline = true;
            break;
            case 8:
            //Condition - Trigger
            dialog_lastcondition = 8;
            break;
            case 9:
            //Condition - Variable
            dialog_lastcondition = 9;
            break;
            case 10:
            //End of a block
            dialog_lastcondition = -1;
            dialog_nextline = true;
            break;
            case 11:
            //Multiple options
            menu_create("dlgoptions",view_wview[0]-32-(string_get_longest("|",dialog_value[1])),16+string_height_ext(ds_list_find_value(dialog_queue[1],0),-1,296));
            dialog_menu_variable = dialog_value[0];
            for (n = 0; n < string_count("|",dialog_value[1])+1; n += 1)
            {
                menu_add_option("dlgoptions",n,explode(n,"|",dialog_value[1]));
            }
            dialog_nextline = true;
            break;
            case 12:
            //Condition - Team member
            dialog_lastcondition = 12;
            break;
        }
    }
    if (dialog_lineended == true) && (is_real(global.menu_id)) 
    {
        if (global.button_anykey_pressed == true) && (dialog_timer == 0)
        {
            dialog_nextline = true;
        }
        if (dialog_textposition[0] > 1) && (menu_exists("dlgoptions") == true)
        {
            menu_select("dlgoptions");
        }
    }
    if (dialog_linetype == 0)
    {
        dialog_active = false;
        dialog_paused = false;
        dialog_text[0] = "";
        dialog_text[1] = "";
        dialog_lasttext = "";
        dialog_textposition[0] = 0;
        dialog_textposition[1] = 0;
        dialog_linetype = -1;
        dialog_value[0] = -1;
        dialog_value[1] = -1;
        ds_list_clear(dialog_queue[0]);
        ds_list_clear(dialog_queue[1]);
        ds_list_clear(dialog_queue[2]);
        ds_map_clear(global.shop_items);
        dialog_nextline = false;
        dialog_lineended = false;
        dialog_menu_variable = -1;
        dialog_lastcondition = -1;
        dialog_pausetime = 0;
        global.control = 1;
    }
}
else
{
    if (ds_list_empty(global.team) == false) && (global.team_member_current != -1)
    {
        if ((ds_list_find_value(global.team,global.team_member_current).character_talk) && (global.control == 1) && (global.inventory == false) && (global.cutscene == false))
        {
            dialog_get_lines(ds_list_find_value(global.team,global.team_member_current).character_talking_to);
            dialog_get_shopitems(ds_list_find_value(global.team,global.team_member_current).character_talking_to);
            if ((ds_list_size(dialog_queue[0]) > 0))
            {
                global.control = 0;
            }
        }
    }
}
